<?php
//require_once '/user.class.php';
/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

require_once 'sql_helper.class.php';

class Booking
{
    public static function getSession($robotID, $startTime, $endTime)
    {
        $startTime = $startTime.":00:00";
        $endTime = $endTime.":00:00";
        $statement = "SELECT * FROM `C-dev`.SESSION WHERE robotID=$robotID AND startTime='$startTime' AND endTime='$endTime'";
        $result = SQLHelper::executeQuerry($statement);

        $row = mysql_fetch_object($result);

        if ($row != null)
            return $row->sessionID;
        else
            return null;
    }

    public static function addBooking($userID, $robotID, $bookingTime)
    {
       $time = explode('_',$bookingTime);
       $day = $time[0];
       $startTime = $time[1];
       $endTime = $time[2];
       if ($day == 'mon')
           $day = 'monday';
       if ($day == 'tue')
            $day = 'tuesday';
       if ($day == 'wed')
            $day = 'wednesday';
       if ($day == 'thu')
            $day = 'thursday';
       if ($day == 'fri')
            $day = 'friday';

       $day = $day." this week";

       $sessionID = Booking::getSession($robotID, $startTime, $endTime);

       $date = date("Y-m-d", strtotime($day));
       $statement = "INSERT INTO `C-dev`.USER_SESSION VALUE($userID, $sessionID, '$date')";
       SQLHelper::executeNonQuerry($statement);

       header("Location: index.php?message=Booking Successfully in $date From $startTime to $endTime!");
    }
}